import {
  getCustomerTable
} from '../../api/customer'
import {
  getSaleCount
} from '../../api/financial'
import {
  SIGN_LIST,
  SALE_COUNT,
  SIGN_DATA_COUNT,
  SIGN_HISTORY_DATA,
  SIGN_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    signList: [],
    saleCount: [],
    signDataCount: null,
    signPageSize: 20,
    signHistoryData: [],
    row: {
      uid: '',
      ctime: '',
      uname: '',
      tel: '',
      coludname: '',
      coludtel: '',
      mname: '',
      appointime: '',
      state: '',
      comdate: '',
      tname: '',
      gid: '',
      gname: '',
      orderdate: '',
      cprice: '',
      commission: '',
      state0: '',
      ttype: '',
      remarks: ''
    }
  },
  getters: {
    signList: state => state.signList
  },
  mutations: {
    [SIGN_LIST] (state, data) {
      state.signList = data
    },
    [SALE_COUNT] (state, data) {
      state.saleCount = data
    },
    [SIGN_DATA_COUNT] (state, data) {
      state.signDataCount = data
    },
    [SIGN_HISTORY_DATA] (state, data) {
      state.signHistoryData = data
    },
    [SIGN_PAGE_SIZE] (state, data) {
      state.signPageSize = data
    }
  },
  actions: {
    async getSignList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let sumSaleMoney = ''
      let sumMonthSaleMoney = ''
      let sumDateSaleMoney = ''
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getCustomerTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        // if (item.state === 5) {
        newData = [...newData, {
          id: item.id,
          uid: item.uid,
          ctime: item.ctime,
          uname: item.uname,
          tel: item.tel,
          coludname: item.coludname,
          coludtel: item.coludtel,
          mname: item.mname,
          appointime: item.appointime,
          state: item.state,
          comdate: item.comdate,
          tname: item.tname,
          gid: item.gid,
          gname: item.gname,
          orderdate: item.orderdate,
          cprice: item.cprice,
          commission: item.commission,
          state0: item.state0,
          ttype: item.ttype,
          remarks: item.remarks
        }]
        // }
      })
      commit(SIGN_LIST, newData)
      commit(SIGN_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.sign.signPageSize) {
        commit(SIGN_HISTORY_DATA, newData)
      } else {
        commit(SIGN_HISTORY_DATA, newData.slice(0, rootState.sign.signPageSize))
      }
      return resp
    },
    async changeSignPage ({ commit, rootState }, index) {
      let data = rootState.sign.signList
      let _start = (index - 1) * rootState.sign.signPageSize
      let _end = index * rootState.sign.signPageSize
      commit(SIGN_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeSignPageSize ({ commit, rootState }, value) {
      commit(SIGN_PAGE_SIZE, value)
    },
    async getSaleCountData ({ commit, rootState }) {
      const resp = await getSaleCount()
      // console.log(resp)
      commit(SALE_COUNT, resp.data.data)
    }
  }
}
